package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Stock;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ICommodityService;
import cn.wolfcode.trip.base.service.IStockService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("stock")
public class StockController {

    @Autowired
    private IStockService stockService;

    @Autowired
    private ICommodityService commodityService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo")QueryObject qo, Model model){
        model.addAttribute("pageInfo",stockService.query(qo));
        model.addAttribute("commodities",commodityService.list());
        return "commodity/stock";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Stock stock) {
        JSONResult jsonResult = new JSONResult();
        try{
            stockService.saveOrUpdate(stock);
        }catch (Exception e){
            return jsonResult.mark(e.getMessage());
        }
        return jsonResult; //象征性返回操作成功
    }

}
